clear
set more off
*version 8
capture log close


use "C:\Users\Andy baker\OneDrive - UCB-O365\Data\Mexico\Mexico 2006\base panel mexico 2006.dta", clear
gen respond1=1 if dia~=.
gen respond2=1 if dia_2~=.
gen respond3=1 if dia_3~=.
recode respond* (.=0)

gen pweight=1
replace pweight=160/(160+500) if estado==9
replace pweight=80/(80+140) if estado==7
replace pweight=100/(80+100) if estado==14
replace pweight=60/(80+60) if estado==20	

*note that those in the cross-sectional samples answered shorter questionnaires and were not reinterviewed
drop if tipomu_2==4
drop if tipomu_3==4

*-------------------------------------------------
*MERGE IN STATE DATA
replace estado=estado_2 if estado==.
replace estado=estado_3 if estado==.
sort estado

merge m:m estado using "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 6 Regionalism\Data\nationalization_mex2000_mergetoindy.dta"
gen other2000=1-(pan2000+prd2000+pri2000)
*gen prdvspan2000=prd2000/(prd2000+pan2000)
*gen privspan2000=pri2000/(pri2000+pan2000)
replace prd2000=prd2000/(pan2000+prd2000+pri2000)
replace pan2000=pan2000/(pan2000+prd2000+pri2000)
replace pri2000=pri2000/(pan2000+prd2000+pri2000)
sort estado
drop _m

merge m:m estado using "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 6 Regionalism\Data\nationalization_mex2006_mergetoindy.dta"


*----------------------------------------------------------

recode p8 2=3 3=2 4/5=7 6=7 7/9=7 .=7, gen(vote_iv1)
label define vote_iv1 1 "Felipe Calderon" 3 "Roberto Madrazo" 2 "AMLO" 7 "Others or Abstain/NR"
label values vote_iv1 vote_iv1
replace vote_iv1=. if respond1~=1

recode p7_2 4=6 6=6 7/9=7 .=7, gen(vote2)
label define vote 1 "Felipe Calderon" 2 "Roberto Madrazo" 3 "AMLO" 6 "Others" 7 "Abstain/NR"
label values vote2 vote
replace vote2=. if respond2~=1

recode p6_3 4/6=6 7/9=7 .=7, gen(vote)
replace vote=. if respond3~=1
label values vote vote

*DEFINE INDEPENDENT VARIABLES
recode p22 2=.5 1=1 else=0, gen(panista)
recode p22 3=1 4=.5 else=0, gen(priista)
recode p22 5=1 6=.5 else=0, gen(prdista)
recode p22 7/9=1 else=0, gen(party_base)

recode p26 1=5 2=4 3=3 4=2 5=1 6=., gen(p26foxapproval)
gen p21felipe = (p21f)/10 
gen p21amlo = (p21h)/10 
gen p21madrazo = (p21i)/10 

gen religion = p58 
recode religion (1=4) (2=3) (3=2) (4=1) (5=0) (6=.) 
gen educ = p59 
recode educ (1=0) (2=1) (3=2) (4=3) (5=4) (6=5) (7=6) (8=7) (9=8) (10=.) 
recode p64a-p64h (1=1) (2=0) (else=.)
irt 2pl p64a-p64h
predict wealth, ebmeans latent
recode a 2=1 1=0, gen(woman)
rename d age
recode p2_2 1=4 2=3 3=2 4=1 else=.,gen(interest2)
recode p3_2 1=5 2=4 3=3 4=2 5=1 else=., gen(converse2)
recode p47_3 1=5 2=4 3=3 4=2 5=1 else=., gen(converse3)
recode p2_3 1=4 2=3 3=2 4=1 else=.,gen(interest3)
recode p70_2 11=., gen(income2)
recode p4_2 1=4 2=3 3=2 4=1 else=., gen(interestcamp2)
recode p3_3 1=4 2=3 3=2 4=1 else=., gen(interestcamp3)
/* ADDING UP p71 and p72 */ 
gen z = p71 
recode z (1=1) (2=0) (3=0) (4=0) 
gen y = p72a 
recode y (2=0) (.=0) 
gen x = p72b 
recode x (2=0) (.=0) 
gen w = p72c 
recode w (2=0) (.=0) 
gen v = p72d 
recode v (2=0) (.=0)   
gen aware = v+w+x+y+z  
drop v 
drop w 
drop x 
drop y 
drop z 


/* QUESTION #39/40 */ 

gen capital = 4 if p39==1 & p40==1 
replace capital = 3 if [p39==1 & p40==2] | [p39==1 & p40==3] 
replace capital = 2 if p39==3 
replace capital = 1 if [p39==2 & p40==2] | [p39==2 & p40==3] 
replace capital = 0 if p39==2 & p40==1  
/* QUESTION #41/42 */ 
gen abortion = 0 if p41==1 & p42==1 
replace abortion = 1 if [p41==1 & p42==2] | [p41==1 & p42==3] 
replace abortion = 2 if p41==3 
replace abortion = 3 if [p41==2 & p42==2] | [p41==2 & p42==3] 
replace abortion = 4 if p41==2 & p42==1  
/* QUESTION #43/44 */ 
gen privatize = 4 if p43==1 & p44==1 
replace privatize = 3 if [p43==1 & p44==2] | [p43==1 & p44==3] 
replace privatize = 2 if p43==3 
replace privatize = 1 if [p43==2 & p44==2] | [p43==2 & p44==3] 
replace privatize = 0 if p43==2 & p44==1  
/* QUESTION #46/47 */ 
gen usatrade = 4 if p46==1 & p47==1 
replace usatrade = 3 if [p46==1 & p47==2] | [p46==1 & p47==3] 
replace usatrade = 2 if p46==3 | p46==4 
replace usatrade = 1 if p46==2 & p47==2 | p46==2 & p47==3 
replace usatrade = 0 if p46==2 & p47==1 

recode tipo 1=3 2=1 3=2, gen(urban)

char p8[omit] 8 
xi i.p8
renpfix _Ip8_ lagvote
char vote[omit] 5
xi i.vote
renpfix _I

char p7_2[omit] 8 
xi i.p7_2
renpfix _Ip7_2_ lagvotet

char vote_iv1[omit] 1
xi i.vote_iv1
renpfix _I


*DISCUSSANT IVS
*WAVE 2
	*strong definition
recode p68a_2-p68c_2 (1=1) (else=0), gen(felipe21 felipe22 felipe23)
recode p68a_2-p68c_2 (2=1) (else=0), gen(madrazo21 madrazo22 madrazo23)
recode p68a_2-p68c_2 (3=1) (else=0), gen(amlo21 amlo22 amlo23)
recode p68a_2-p68c_2 (4=1) (else=0), gen(campa21 campa22 campa23)
recode p68a_2-p68c_2 (5=1) (else=0), gen(mercado21 mercado22 mercado23)
egen felipedisc2=rsum(felipe2*)
egen madrazodisc2=rsum(madrazo2*)
egen amlodisc2=rsum(amlo2*)
egen campadisc2=rsum(campa2*)
egen mercadodisc2=rsum(mercado2*)
egen totopinionateddisc2=rsum(felipe21 felipe22 felipe23 madrazo21 madrazo22 madrazo23 amlo21 amlo22 amlo23 campa21 campa22 campa23 mercado21 mercado22 mercado23)
replace totopinionateddisc2=. if respond2~=1
gen felipediscper2=felipedisc2/totopinionateddisc2
gen madrazodiscper2=madrazodisc2/totopinionateddisc2
gen amlodiscper2=amlodisc2/totopinionateddisc2
gen otherdiscper2=(campadisc2+mercadodisc2)/totopinionateddisc2
recode *discper2 (.=0)
recode totopinionateddisc2 0=1 1/3=0 .=., gen(zerodiscstrong2)

	*extra stuff for compositional analysis
replace felipediscper2 =. if respond2~=1
replace madrazodiscper2 =. if respond2~=1
replace amlodiscper2 =. if respond2~=1
replace zerodiscstrong2=. if respond2~=1 
replace otherdiscper2=. if respond2~=1

	*weak definition
egen numdisc2=rsum( p66a_2 p66b_2 p66c_2)
replace numdisc2=. if respond2~=1
gen felipediscperweak2=felipedisc2/numdisc2
gen madrazodiscperweak2=madrazodisc2/numdisc2
gen amlodiscperweak2=amlodisc2/numdisc2
gen otherdiscperweak2=(campadisc2+mercadodisc2)/numdisc2
gen nonediscperweak2=1-(felipedisc2+madrazodisc2+amlodisc2+campadisc2+mercadodisc2)/numdisc2
replace felipediscperweak2=0 if numdisc2==0 & felipediscperweak2==.
replace madrazodiscperweak2=0 if numdisc2==0 & madrazodiscperweak2==.
replace amlodiscperweak2=0 if numdisc2==0 & amlodiscperweak2==.
replace otherdiscperweak2=0 if numdisc2==0 & otherdiscperweak2==.
replace nonediscperweak2=0 if numdisc2==0 & nonediscperweak2==.
tab felipediscperweak2 felipediscper2, m
recode numdisc2 0=1 1/3=0 .=., gen(zerodiscweak2)

/**WAVE 3
recode p50a_3-p50c_3 (1=1) (else=0), gen(felipe31 felipe32 felipe33)
recode p50a_3-p50c_3 (2=1) (else=0), gen(madrazo31 madrazo32 madrazo33)
recode p50a_3-p50c_3 (3=1) (else=0), gen(amlo31 amlo32 amlo33)
recode p50a_3-p50c_3 (4=1) (else=0), gen(campa31 campa32 campa33)
recode p50a_3-p50c_3 (5=1) (else=0), gen(mercado31 mercado32 mercado33)
egen felipedisc3=rsum(felipe3*)
egen madrazodisc3=rsum(madrazo3*)
egen amlodisc3=rsum(amlo3*)
egen campadisc3=rsum(amlo3*)
egen mercadodisc3=rsum(amlo3*)
egen totopinionateddisc3=rsum(felipe31 felipe32 felipe33 madrazo31 madrazo32 madrazo33 amlo31 amlo32 amlo33 campa31 campa32 campa33 mercado31 mercado32 mercado33)
replace totopinionateddisc3=. if respond3~=1
gen felipediscper3=felipedisc3/totopinionateddisc3
gen madrazodiscper3=madrazodisc3/totopinionateddisc3
gen amlodiscper3=amlodisc3/totopinionateddisc3
recode *discper3 (.=0)
recode totopinionateddisc3 0=1 1/3=0 .=., gen(zerodiscstrong3)

replace felipediscper3 =. if respond3~=1
replace madrazodiscper3 =. if respond3~=1
replace amlodiscper3 =. if respond3~=1
replace zerodiscstrong3=. if respond3~=1 
egen numdisc3=rsum(p48a_3 p48b_3 p48c_3)
replace numdisc3=. if respond3~=1
gen felipediscperweak3=felipedisc3/numdisc3
gen madrazodiscperweak3=madrazodisc3/numdisc3
gen amlodiscperweak3=amlodisc3/numdisc3
replace felipediscperweak3=0 if numdisc3==0 & felipediscperweak3==.
replace madrazodiscperweak3=0 if numdisc3==0 & madrazodiscperweak3==.
replace amlodiscperweak3=0 if numdisc3==0 & amlodiscperweak3==.
tab felipediscperweak3 felipediscper3, m
recode numdisc3 0=1 1/3=0 .=., gen(zerodiscweak3)
*/

recode b 4=. 5=., gen(ethnic)

*PARTY CLIENTELISM
recode p30a_3-p30g_3 (1/6=1) (else=.)
gen pan_client3=1 if p30a_3==1 | p30e_3==1
gen pri_client3=1 if p30b_3==1 | p30f_3==1
gen prd_client3=1 if p30c_3==1 | p30g_3==1
gen other_client3=1 if p30h_3==1 | p30d_3==1
recode pan_client3 pri_client3 prd_client3 other_client3 (.=0)

recode p42a_2-p42g_2 (1/6=1) (else=.)
gen pan_client2=1 if p42a_2==1 | p42e_2==1
gen pri_client2=1 if p42b_2==1 | p42f_2==1
gen prd_client2=1 if p42c_2==1 | p42g_2==1
gen other_client2=1 if p42h_2==1 | p42d_2==1
recode pan_client2 pri_client2 prd_client2 other_client2 (.=0)

*PARTY CONTACTING 
gen pan_contact3=1 if p28a_3==1 | p28e_3==1
gen pri_contact3=1 if p28b_3==1 | p28f_3==1
gen prd_contact3=1 if p28c_3==1 | p28g_3==1
gen other_contact3=1 if p28d_3==1 | p28h_3==1
recode pan_contact3 pri_contact3 prd_contact3 other_contact3 (.=0)

gen pan_contact2=1 if p41a_2==1 | p41e_2==1
gen pri_contact2=1 if p41b_2==1 | p41f_2==1
gen prd_contact2=1 if p41c_2==1 | p41g_2==1
gen other_contact2=1 if p41h_2==1 | p41h_2==1
recode pan_contact2 pri_contact2 prd_contact2 other_contact2 (.=0)

gen pan_contact=1 if pan_client3==1 | pan_contact3==1 | pan_client2==1 | pan_contact2==1
gen pri_contact=1 if pri_client3==1 | pri_contact3==1 | pri_client2==1 | pri_contact2==1
gen prd_contact=1 if prd_client3==1 | prd_contact3==1 | prd_client2==1 | prd_contact2==1
recode pan_contact pri_contact prd_contact (.=0)

*MEDIA
label define tv 0 "none" 1 "televisa" 2 "azteca" 3 "other"

recode p13a_3 1=4 2=3 3=2 4=1, gen(tv_program1_freq)
recode p13b_3 1=4 2=3 3=2 4=1, gen(tv_program2_freq)
 
recode p12a_3 1=0 2=3 3=2 4=3 5=3 6=3 7=1 8=1 9/13=2 14=3 15=1 ///
16=1 17=1 18/19=3 20=1 21=2 22=3 23=0 24=3 25=3 26=1 27=1 28=3 29=3 30=3 31=3 32=3 .=., gen(tv_program1)
label values tv_program1 tv 
 
recode p12b_3 1=0 2=3 3=2 4=3 5=3 6=3 7=1 8=1 9/13=2 14=3 15=1 ///
16=1 17=1 18/19=3 20=1 21=2 22=3 23=0 24=3 25=3 26=1 27=1 28=3 29=3 30=3 31=3 32=3 .=., gen(tv_program2)
label values tv_program2 tv 

gen televisa_expose=0 if respond3==1
replace televisa_expose=tv_program1_freq if tv_program1==1 & tv_program2~=1
replace televisa_expose=tv_program2_freq if tv_program2==1 & tv_program1~=1
replace televisa_expose=tv_program1_freq + tv_program2_freq  if tv_program1==1 & tv_program2==1

gen azteca_expose=0 if respond3==1
replace azteca_expose=tv_program1_freq if tv_program1==2 & tv_program2~=2
replace azteca_expose=tv_program2_freq if tv_program2==2 & tv_program1~=2
replace azteca_expose=tv_program1_freq + tv_program2_freq  if tv_program1==2 & tv_program2==2

gen other_expose=0 if respond3==1
replace other_expose=tv_program1_freq if tv_program1==3 & tv_program2~=3
replace other_expose=tv_program2_freq if tv_program2==3 & tv_program1~=3
replace other_expose=tv_program1_freq + tv_program2_freq  if tv_program1==3 & tv_program2==3

*REGS
label variable pan2000 "PAN's 2000 vote share in R’s state"
label variable prd2000 "PRD's 2000 vote share in R’s state"
label variable pri2000 "PRI's 2000 vote share in R’s state"
label variable other2000 "Other parties' 2000 vote share in R's state"
label variable panista "Panista(pc wave)"
label variable prdista "Perredista(pc wave)"
label variable priista "Priista(pc wave)"
label variable party_base "Partisans of other party and nonpartisans(pc wave)"
label variable lagvote1 "Vote intention for Calderón(pc wave)"
label variable lagvote3 "Vote intention for AMLO(pc wave)"
label variable lagvote2 "Vote intention for Madrazo(pc wave)"
label variable urban "Urban resident"
label variable educ "Education level"
label variable wealth "Wealth"
label variable ethnic "Skin color"
label variable woman "Woman"
label variable religion "Frequency of church attendance"
label variable zerodiscstrong2 "Isolates"
label variable usatrade "Support for more trade with U.S.(pc wave)"
label variable privatize "Support for private investment in electricity sector (pc wave)"
label variable abortion "Support for abortion rights if raped (pc wave)"
label variable capital "Support for capital punishment (pc wave)"
label variable pan_contact "Contacted by PAN (e wave)"
label variable pri_contact "Contacted by PRI (e wave)"
label variable prd_contact "Contacted by PRD (e wave)"
label variable televisa_expose "Televisa news exposure (e wave)"
label variable azteca_expose "TV Azteca news exposure (e wave)"
label variable other_expose "Other network news exposure (e wave)"
label variable amlodiscper2 "Share of discussants that are pro-AMLO"
label variable madrazodiscper2 "Share of discussants that are pro-Madrazo"
label variable otherdiscper2 "Share of discussants that are pro-other candidate"

*Discussant Choice


local PRDguer=.36011/(.36011+.436+.18972)
local PRIguer=.436/(.36011+.436+.18972)

local PRDguan=.06717/(.06717+.2863+.62416)
local PRIguan=.2863/(.06717+.2863+.62416)

*/

*Strong definition of disagreement	
eststo clear 
*Figure 6.7, Table A.16

eststo: fmlogit felipediscper2 amlodiscper2 madrazodiscper2 otherdiscper2 if zerodiscstrong2==0 [pweight=pweight], ///
etavar(prd2000 pri2000 other2000 prdista priista party_base ib1.vote_iv1 urban educ wealth woman ethnic ) cluster(municipi)

margins , at(prd2000=`PRDguan' pri2000=`PRIguan' other2000=0 (mean) party_base prdista priista 2.vote_iv1 3.vote_iv1 7.vote_iv1 urban educ wealth woman ethnic ) predict(outcome(felipediscper2)) predict(outcome(amlodiscper2 )) predict(outcome(madrazodiscper2)) predict(outcome(otherdiscper2))
margins , at(prd2000=`PRDguer' pri2000=`PRIguer' other2000=0 (mean) party_base prdista priista 2.vote_iv1 3.vote_iv1 7.vote_iv1 urban educ wealth woman ethnic ) predict(outcome(felipediscper2)) predict(outcome(amlodiscper2 )) predict(outcome(madrazodiscper2)) predict(outcome(otherdiscper2))

cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 6 Regionalism\Code\Output\"
esttab est1 using Mexico2006_disc_choice.rtf, label b(3) replace se star(* 0.05 ) nogap onecell ///
title(Table 5.2. The Correlates of Discussant Choice in Mexico 2006) ///
modelwidth(6) mtitles("Pro-Calderón Discussants' Share of Network (c wave)" "Pro-AMLO Discussants' Share of Network (c wave)" "Pro-Madrazo Discussants' Share of Network (c wave)") compress ///
addnote("Entries are quasi maximum likelihood fractional multinomial logit coefficients with robust standard errors (corrected for clustering by state) in parentheses.")

recode vote 6=.

quietly mlogit vote prd2000 pri2000 vote_iv1_2 vote_iv1_3 vote_iv1_7 wealth educ ethnic usatrade privatize religion abortion capital urban woman party_base prdista priista if respond2==1 [pweight=pweight], cluster(municipi) b(1) 
gen esample=1 if e(sample)==1

*Table 6.3, A.18
	
eststo clear	
eststo: mlogit vote prd2000 pri2000 other2000 vote_iv1_2 vote_iv1_3 vote_iv1_7 if respond2==1 & esample==1 [pweight=pweight], b(1) cluster(municipi)
eststo: mlogit vote prd2000 pri2000 other2000 vote_iv1_2 vote_iv1_3 vote_iv1_7 wealth educ ethnic usatrade privatize religion abortion capital urban woman prdista priista party_base if respond2==1 [pweight=pweight], b(1) cluster(municipi)
eststo: mlogit vote prd2000 pri2000 other2000 vote_iv1_2 vote_iv1_3 vote_iv1_7 wealth educ ethnic usatrade privatize religion abortion capital urban woman prdista priista party_base pan_contact prd_contact pri_contact televisa_expose azteca_expose other_expose if respond2==1 [pweight=pweight], b(1) cluster(municipi)
eststo: mlogit vote prd2000 pri2000 other2000 vote_iv1_2 vote_iv1_3 vote_iv1_7 wealth educ ethnic usatrade privatize religion abortion capital urban woman prdista priista party_base pan_contact prd_contact pri_contact televisa_expose azteca_expose other_expose amlodiscper2 madrazodiscper2 otherdiscper2 zerodiscstrong2 if respond2==1 [pweight=pweight], b(1) cluster(municipi)

esttab est1 est2 est3 est4 using Mexico2006_mnls.rtf, label b(3) replace se star(* 0.05 ) nogap onecell ///
title(Table 5.4. Correlates of Presidential Vote Choice in Mexico, 2006) ///
modelwidth(6) compress ///
addnote("Entries are multinomial logit coefficients and standard errors (adjusted for clustering within state) in parentheses. Coefficients for three other dependent variable categories (vote for other and abstention) are not reported to reduce clutter.")
xyz





*For appendix: weak definition
eststo clear
eststo: fmlogit felipediscperweak2 amlodiscperweak2 madrazodiscperweak2 otherdiscperweak2 nonediscperweak2 if zerodiscweak2==0 [pweight=pweight] , ///
etavar(prd2000 pri2000 other2000 prdista priista party_base ib1.vote_iv1 urban educ wealth ethnic woman ) cluster(estado)

margins , at(prd2000=`PRDguan' pri2000=`PRIguan' other2000=`otherguan' (mean) prdista priista party_base 2.vote_iv1 3.vote_iv1 7.vote_iv1 urban educ wealth ethnic woman ) predict(outcome(felipediscperweak2)) predict(outcome(amlodiscperweak2 )) predict(outcome(madrazodiscperweak2)) predict(outcome(otherdiscperweak2)) predict(outcome(nonediscperweak2))
margins , at(prd2000=`PRDguer' pri2000=`PRIguer' other2000=`otherguer' (mean) prdista priista party_base 2.vote_iv1 3.vote_iv1 7.vote_iv1 urban educ wealth ethnic woman ) predict(outcome(felipediscperweak2)) predict(outcome(amlodiscperweak2 )) predict(outcome(madrazodiscperweak2)) predict(outcome(otherdiscperweak2)) predict(outcome(nonediscperweak2))

cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 6 Regionalism\Code\Output\"
esttab est1 using Mexico2006_disc_choice_weak.rtf, label b(3) replace se star(* 0.05 ) nogap onecell ///
title(Table 5.2A. The Correlates of Discussant Choice in Mexico 2006 (Restrictive Definition)) ///
modelwidth(6) mtitles("Pro-Calderón Discussants' Share of Network (c wave)" "Pro-AMLO Discussants' Share of Network (c wave)" "Pro-Madrazo Discussants' Share of Network (c wave)") compress ///
addnote("Entries are quasi maximum likelihood fractional multinomial logit coefficients with robust standard errors (corrected for clustering by state) in parentheses.")

drop esample
quietly mlogit vote prd2000 pri2000 other2000 wealth educ ethnic usatrade privatize religion abortion capital urban woman prdista priista party_base if respond2==1 [pweight=pweight], cluster(estado) b(1) 
gen esample=1 if e(sample)==1

eststo clear

eststo: mlogit vote prd2000 pri2000 other2000 wealth educ ethnic usatrade privatize religion abortion capital urban woman prdista priista party_base pan_contact prd_contact pri_contact televisa_expose azteca_expose other_expose amlodiscperweak2 madrazodiscperweak2 otherdiscperweak2 nonediscperweak2 zerodiscweak2 if respond2==1 [pweight=pweight], cluster(estado) b(1) 
esttab est1 using Mexico2006_mnls_weak.rtf, label b(3) replace se star(* 0.05 ) nogap onecell ///
title(Table 5.4A. Correlates of Presidential Vote Choice in Mexico, 2006 using permissive definition) ///
modelwidth(6) compress ///
addnote("Entries are multinomial logit coefficients and standard errors (adjusted for clustering within state) in parentheses. Coefficients for three other dependent variable categories (vote for other and abstention) are not reported to reduce clutter.")

